package com.chengzhi.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.naming.Name;

@RestController
public class ConsumerController {
	@Autowired
	private RestTemplate restTemplate;

	/**
	 * 默认配置是两个线程  query一个 HystrixCommand一个
	 * HystrixCommand服务容错配置
	 * @return
	 */
	@RequestMapping("query1")
	@HystrixCommand(fallbackMethod = "error")
	public String query1() {
		return restTemplate.getForEntity("http://service/hello/query", String.class).getBody();
	}
	/**
	 * 配置commandProperties  是一个线程
	 */
	@HystrixCommand(fallbackMethod = "error",commandProperties = @HystrixProperty(name = "execution.isolation.strategy", value = "SEMAPHORE"))
	@RequestMapping("query2")
	public String query2() {
		return restTemplate.getForEntity("http://service/hello/query", String.class).getBody();
	}


	public String error() {
		return "error";
	}
}
